import os
import logging

from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import login_required

from listmodel import ListModel
import utils

class UpdateVisibilityListRequestHandler(webapp.RequestHandler):
    def post(self):
        # Retrieve the IDs of the List to update the Visibility of
        list_ids = self.request.get_all("list_id[]")
        # The Visibility to Set
        new_visibility = self.request.get("visibility")
        # Check if this is an Ajax Call or not. Default is not.
        ajax_call = self.request.get("ajax")
        
        # Check that the necessary parameters were passed
        if ( list_ids and new_visibility ):            
            # Current User
            current_user = users.get_current_user()
            
            # Change Visibility to every selected List
            for list_id in list_ids:
                # Retrieve the List from the Datastore
                list = ListModel.get_by_id(int(list_id))
                
                # Check that the Current User owns the given Lists
                if ( utils.isListOwnedByUser(list, current_user) ):
                    list.visibility = new_visibility
                    # Put it back in the Datastore
                    list.put()
            
        if ( ajax_call == "true" ):
            self.redirect('/?ajax=true')
        else:
            self.redirect('/')
        